package com.atguigu.gulimall.order.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.order.dao.OrderReturnReasonDao;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.atguigu.gulimall.order.service.OrderReturnReasonService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@RabbitListener(queues = {"java.queue"})
@Service("orderReturnReasonService")
public class OrderReturnReasonServiceImpl extends ServiceImpl<OrderReturnReasonDao, OrderReturnReasonEntity> implements OrderReturnReasonService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderReturnReasonEntity> page = this.page(
                new Query<OrderReturnReasonEntity>().getPage(params),
                new QueryWrapper<OrderReturnReasonEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 接收消息
     *
     * @param message 原生消息详细信息，头+体
     *                T<发送的消息的类型> OrderReturnReasonEntity entity
     *                Channel channel :当前传输数据的通道
     *                Queue:可以很多人来监听，只要收到消息，队列删除消息，只能有一个人收到此消息
     *                场景：
     *                1）订单服务启动多个：同一个消息，只能有一个客户端接受
     *                2）只要一个消息完全处理完成，方法运行结束，我们就可以接受到下一个消息
     */
//    @RabbitListener(queues = {"java.queue"})
//    @RabbitHandler
    void receiveMessage(Message message, OrderReturnReasonEntity entity, Channel channel) throws InterruptedException {
        byte[] body = message.getBody();
        //消息头属性信息
        MessageProperties messageProperties = message.getMessageProperties();
        System.out.println("接收到消息。。。" + entity);
//        Thread.sleep(3000);
        System.out.println("消息处理完成：" + entity.getName());
        long deliveryTag = messageProperties.getDeliveryTag();
        //deliveryTag在channel中按顺序自增
        //确认签收 非批量签收
        try {
            if (deliveryTag % 2 == 0) {
                System.out.println("deliveryTag==>" + deliveryTag);
                channel.basicAck(deliveryTag, false);
            } else {
                //long var1, boolean var3, boolean var4
                //                         false 丢弃 true 告诉服务器重新入队
                channel.basicNack(deliveryTag,false,true);
                //long var1, boolean var3
//                channel.basicReject();
            }
        } catch (IOException e) {
            //网络中断
            e.printStackTrace();
        }
    }

//    @RabbitHandler
    void receiveMessage2(Message message, OrderEntity entity, Channel channel) {
        System.out.println("接收到消息。。。" + entity);
    }
}